package com.scr1pt.extension

/**
 * 说明: Int类型扩展
 *
 * 创建者: tum0r
 *
 * 时间: 2021/9/11 15:51
 */

/**
 * Int数组表示的IP地址转成Int类型
 */
fun IntArray.getIPInt(): Int {
    var result = 0
    for (i in this)
        result = ((result shl 8) or i)
    return result
}

/**
 * 将Int类型的IP转为Byte数组形式
 */
fun Int.getIPByteArray(): ByteArray {
    val result = ByteArray(4)
    for (i in 0 until 4)
        result[i] = ((this shr (8 * (3 - i))) and 0xff).toByte()
    return result
}

/**
 * 将Int类型的IP转为字符串形式
 */
fun Int.getIPString(): String {
    val result = StringBuilder()
    for (ip in this.getIPByteArray())
        result.append(ip.toPositiveInt()).append(".")
    return result.deleteAt(result.length - 1).toString()
}